import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType

# 1. 加载数据
data = pd.read_excel('成都地区算法岗位工资前十.xlsx')

# 2. 创建柱状图对象
salary = Bar(init_opts=opts.InitOpts(
    width="2000px",
    height="800px",
    theme=ThemeType.MACARONS  # 选择一个美观的主题
))

# 3. 添加X轴和Y轴数据
company_names = []
for name in data['company_name'].tolist():
    # 动态换行处理，如果公司名长度超过10个字符
    if len(name) > 10:
        new_name = name[:10] + '<br>' + name[10:]
    else:
        new_name = name
    company_names.append(new_name)

salary.add_xaxis(company_names)
salary.add_yaxis('工资', [f"¥{sal}" for sal in data['salary'].tolist()], bar_width='60%')

# 4. 设置全局配置项
salary.set_global_opts(
    title_opts=opts.TitleOpts(
        title='<span style="color:#333333;font-size:28px;font-weight:bold;">成都地区算法岗位工资排名Top10的公司</span>',
        subtitle='<span style="color:#666666;font-size:20px;">第一张数据分析图</span>',
        pos_left='center',
        pos_top='30px',
        # 使用HTML标签和CSS样式进一步美化标题
        title_textstyle_opts=opts.TextStyleOpts(color='inherit'),
        subtitle_textstyle_opts=opts.TextStyleOpts(color='inherit')
    ),
    datazoom_opts=opts.DataZoomOpts(
        type_='slider',
        is_realtime=True,
        range_start=0,
        range_end=100,
        filter_mode='none',
        xaxis_index=[0],
        pos_bottom='10px'
    ),
    legend_opts=opts.LegendOpts(
        is_show=True,
        pos_right='10%',
        orient='vertical',
        item_width=20,
        item_height=14,
        textstyle_opts=opts.TextStyleOpts(color='#333333')
    ),
    xaxis_opts=opts.AxisOpts(
        type_='category',
        axislabel_opts=opts.LabelOpts(
            interval=0,
            font_size=16,
            color='#666666',
            rotate=0,
            formatter=JsCode("function(value) {return value;}")
        ),
    axisline_opts=opts.AxisLineOpts(
        is_show=True,
        line_style=opts.LineStyleOpts(
            color='#999999',
            width=2,
            shadow_color='rgba(0, 0, 0, 0.3)',
            shadow_blur=5,
            shadow_offset_x=2,
            shadow_offset_y=2,
        )
        ),
        axistick_opts=opts.AxisTickOpts(
            is_show=True,
            length=8,
            line_style=opts.LineStyleOpts(
                color='#999999'
            )
        )
    ),
    yaxis_opts=opts.AxisOpts(
        axislabel_opts=opts.LabelOpts(
            interval=0,
            font_size=16,
            color='#666666',
            formatter=JsCode("function(value) {return '¥' + value;}")
        ),
        axisline_opts=opts.AxisLineOpts(
            is_show=True,
            line_style=opts.LineStyleOpts(
                color='#999999',
                width=2,
                shadow_color='rgba(0, 0, 0, 0.3)',
                shadow_blur=5,
                shadow_offset_x=2,
                shadow_offset_y=2
            )
        ),
        axistick_opts=opts.AxisTickOpts(
            is_show=True,
            length=8,
            line_style=opts.LineStyleOpts(
                color='#999999'
            )
        )
    ),
    tooltip_opts=opts.TooltipOpts(
        trigger='axis',
        axis_pointer_type='cross',
        formatter=JsCode("""
            function (params) {
                return '<div style="background-color:#f8f8f8;padding:15px;border-radius:10px;">' +
                       '<p style="color:#333333;font-size:20px;"><strong>' + params[0].name + '</strong></p>' +
                       '<p style="color:#666666;font-size:18px;">工资: ¥' + params[0].data + '</p>' +
                       '</div>';
            }
        """)
    ),
    toolbox_opts=opts.ToolboxOpts(
        is_show=True,
        pos_left="70%",
        pos_top="20px",
        feature={
            "restore": {},
            "saveAsImage": {},
            "dataView": {},
            "magicType": {"type": ["bar", "line"]},
            "brush": {"type": "rect"}
        },
        iconstyle_opts=opts.IconStyleOpts(
            color='#777',
            border_color='#999',
            border_width=1,
        )
    )
)

# 5. 设置系列配置项
salary.set_series_opts(
    itemstyle_opts=opts.ItemStyleOpts(
        color=JsCode(
            """
            function(params) {
                var colorList = [
                    '#FF9999', '#99FF99', '#9999FF', '#FFCC99', '#99CCFF',
                    '#CC99FF', '#FF99CC', '#CCFF99', '#99FFCC', '#FFCCFF'
                ];
                return colorList[params.dataIndex % colorList.length];
            }
            """
        ),
        border_color='#999999',
        border_width=1,
        shadow_color='rgba(0, 0, 0, 0.3)',
        shadow_blur=5,
    ),
    label_opts=opts.LabelOpts(
        is_show=True,
        position='top',
        font_size=16,
        font_family='Arial',
        font_style='normal',
        font_weight='bold',
        color='#333333',
        background_color='rgba(255, 255, 255, 0.8)',
        border_color='#999999',
        border_width=1,
        formatter=JsCode("function(value) {return '¥' + value;}")
    )
)

# 6. 渲染图表到HTML文件
salary.render('成都地区算法岗位工资top10.html')